package uit.sample.interceptors;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class LoggingInterceptor implements MethodBeforeAdvice ,AfterReturningAdvice  {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(method + "logging before!!");
    }

	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
//			System.out.println(arg1 + "afterReturning!");		
	}

	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		System.out.println("Beginning method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
	    long startTime = System.currentTimeMillis();
	    try
	    {
	      Object retVal = methodInvocation.proceed();
	      return retVal;
	    }
	    finally
	    {
	      System.out.println("Ending method: "  + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
	      System.out.println("Method invocation time: " + (System.currentTimeMillis() - startTime) + " msecs.");
	    }

	}
	
	
    
    
}
